package cn.ekuma.epos.crm.link;

import cn.ekuma.epos.crm.model.CustomerScoreDiaryTableModel;
import cn.ekuma.epos.datalogic.I_DataLogicSales;
import com.openbravo.bean.BusinessPartner;
import com.openbravo.bean.crm.CustomerScoreDiary;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.loader.query.QBFCompareEnum;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.panels.event.TimeCycleChangedEvent;

public class JCustomerScoreDiaryLinkPanel extends AbstractCustomerLinkTablePanel<CustomerScoreDiary>{

	Object[] afilter;
	public JCustomerScoreDiaryLinkPanel(I_DataLogicSales dlSales) {
		super(dlSales, new CustomerScoreDiaryTableModel());
	}

	@Override
	public void timeCycleChanged(TimeCycleChangedEvent e) {
		if(afilter==null)
			return;
		  afilter[0] = (e.getStartDate()== null)? QBFCompareEnum.COMP_NONE: QBFCompareEnum.COMP_GREATEROREQUALS;
	      afilter[1] = e.getStartDate();
	      afilter[2] = ( e.getEndDate() == null) ? QBFCompareEnum.COMP_NONE: QBFCompareEnum.COMP_LESS;
	      afilter[3] = e.getEndDate();
	      query();
	}
	
	
	@Override
	public void baseBeanChanged(BusinessPartner o) {
		if(afilter==null)
			try {
				afilter=(Object[]) createValue();
			} catch (BasicException e) {
				e.printStackTrace();
			}
	   afilter[4] = QBFCompareEnum.COMP_EQUALS;
       afilter[5] = o.getId();
       query();
		
	}

	@Override
	public Object createValue() throws BasicException {
		Object[] obj=new Object[6];
		obj[0] = (timerFilter.getStartDate()== null)? QBFCompareEnum.COMP_NONE: QBFCompareEnum.COMP_GREATEROREQUALS;
		obj[1] = timerFilter.getStartDate();
		obj[2] = ( timerFilter.getEndDate() == null) ? QBFCompareEnum.COMP_NONE: QBFCompareEnum.COMP_LESS;
		obj[3] = timerFilter.getEndDate();
		obj[4] = QBFCompareEnum.COMP_NONE;
		obj[5] = null;
	    return obj;
	}
	
	
	public void query(){
		if(afilter==null)
			return;
		try {
			tableModel.resert(dlSales.listByFilter(CustomerScoreDiary.class,afilter));
		} catch (BasicException e) {
			new MessageInf(e).show(this);
		}
	}
	
	public static String getTabName(){
		return AppLocal.getIntString("label.CustomerScoreDiary");
	}

}
